package org.xshop.web.biz;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xshop.model.TGoods;
import org.xshop.model.TGoodsAttribute;
import org.xshop.service.ITGoodsClassifyService;
import org.xshop.service.ITGoodsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.UploadUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 商品  前端控制器
 * </p>
 *
 * @author ShenHuaJie123
 * @since 2018-03-01
 */
@RestController
@RequestMapping("/goods")
@Api(value = "商品接口", description = "商品接口")
public class TGoodsController extends BaseController<TGoods, ITGoodsService> {
    @Autowired
    private ITGoodsClassifyService goodsClassifyService;

    @RequiresPermissions("goods.read")
    @PutMapping(value = "/read/list")
    @ApiOperation(value = "查询商品", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object query(HttpServletRequest request) {
        Map<String, Object> param = WebUtil.getParameter(request);
        return super.query(param);
    }

    @RequiresPermissions("goods.read")
    @PutMapping(value = "/read/detail")
    @ApiOperation(value = "商品详情", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object get(HttpServletRequest request) {
        TGoods param = WebUtil.getParameter(request, TGoods.class);
        TGoods result = service.queryById(param.getId());
        ModelMap modelMap = new ModelMap();
        if (result != null) {
            if (result.getTypeIdI() != null) {
                Map<String, Object> map = InstanceUtil.newHashMap("parentId", result.getTypeIdI().toString());
                List<?> list = goodsClassifyService.queryList(map);
                modelMap.put("typeIdIis", list);
            }
            if (result.getTypeIdIi() != null) {
                Map<String, Object> map = InstanceUtil.newHashMap("parentId", result.getTypeIdIi().toString());
                List<?> list = goodsClassifyService.queryList(map);
                modelMap.put("typeIdIiis", list);
            }
        }
        return setSuccessModelMap(modelMap, result);
    }

    @PostMapping
    @RequiresPermissions("goods.update")
    @ApiOperation(value = "修改商品", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request) {
        TGoods param = WebUtil.getParameter(request, TGoods.class);
        String filePath = UploadUtil.getUploadDir(request);
        String name = param.getPicture();
        if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
            String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
            param.setPicture(path);
        }
        name = param.getImgs();
        if (StringUtils.isNotBlank(name)) {
            String[] namges = name.split(",");
            StringBuilder sb = new StringBuilder();
            for (String file : namges) {
                if (StringUtils.isNotBlank(file) && !file.toLowerCase().contains("http")) {
                    String path = UploadUtil.remove2FDFS(filePath + file).getRemotePath();
                    sb.append(sb.length() == 0 ? path : "," + path);
                } else {
                    sb.append(sb.length() == 0 ? file : "," + file);
                }
            }
            param.setImgs(sb.toString());
        }
        name = param.getGoodsImages();
        if (StringUtils.isNotBlank(name)) {
            String[] namges = name.split(",");
            StringBuilder sb = new StringBuilder();
            for (String file : namges) {
                if (StringUtils.isNotBlank(file) && !file.toLowerCase().contains("http")) {
                    String path = UploadUtil.remove2FDFS(filePath + file).getRemotePath();
                    sb.append(sb.length() == 0 ? path : "," + path);
                } else {
                    sb.append(sb.length() == 0 ? file : "," + file);
                }
            }
            param.setGoodsImages(sb.toString());
        }
        if (param.getAttributes() != null) {
            for (TGoodsAttribute attribute : param.getAttributes()) {
                name = attribute.getAttributePicture();
                if (StringUtils.isNotBlank(name) && !name.toLowerCase().contains("http")) {
                    String path = UploadUtil.remove2FDFS(filePath + name).getRemotePath();
                    attribute.setAttributePicture(path);
                }
            }
        }
        return super.update(param);
    }

    @DeleteMapping
    @RequiresPermissions("goods.delete")
    @ApiOperation(value = "删除商品", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object del(HttpServletRequest request) {
        TGoods param = WebUtil.getParameter(request, TGoods.class);
        Assert.notNull(param.getId(), "ID不能为空");
        return super.delete(param);
    }

    @PostMapping("/check")
    @ApiOperation(value = "审核商品")
    @RequiresPermissions("goods.update")
    public Object check(HttpServletRequest request) {
        TGoods model = WebUtil.getParameter(request, TGoods.class);
        TGoods param = new TGoods();
        param.setId(model.getId());
        param.setEnable(1);
        param.setIsSale(1);
        Long userId = getCurrUser();
        if (param.getId() == null) {
            param.setCreateBy(userId);
            param.setCreateTime(new Date());
        }
        param.setUpdateBy(userId);
        param.setUpdateTime(new Date());
        service.updateData(param);
        return setSuccessModelMap();
    }
}
